-- @owner: lihongji
-- @date: 2022/07/14
-- @testpoint: next_day函数作为if判断条件

--step1:建表;expect:成功
drop table if exists t_ustore_nextday_0047;
create table t_ustore_nextday_0047(
col_1 bigint, col_2 timestamp without time zone, col_3 bool,col_4 decimal,col_5 text ,col_6 smallint ,col_7 char(30),col_8 double precision,col_9 text,col_10 character varying(30),
col_11 bool ,col_12 bytea ,col_13 real ,col_14 numeric ,col_15 blob ,col_16 integer ,col_17 int ,col_18 timestamp with time zone ,col_19 binary_integer ,col_20 interval day to second ,
col_21 boolean, col_22 nchar(30) ,col_23 bigint ,col_24 nchar(100) ,col_25 character(1000) , col_26 text , col_27 float ,col_28 double precision ,col_29 bigint ,col_30 timestamp with time zone ,
col_31 timestamp , col_32 clob ,col_33 interval year to month, col_34 character(30) ,col_35 smallint ,col_36 blob ,col_37 char(300),col_38 float ,col_39 raw(100),col_40 clob ,
col_41 binary_double ,col_42 number(6,2) ,col_43 decimal(6,2) ,col_44 varchar2(50),col_45 varchar(30) ,col_46 nvarchar2(100), col_47 numeric(12,6),col_48 varchar2(50),col_49 date,col_50 clob ,
col_51 integer ,col_52 binary_double ,col_53 decimal(12,6),col_54 raw(8000),col_55 clob ,col_56 varchar2(8000) ,col_57 date ,col_58 number(12,6),col_59 nvarchar2(4000) ,col_60 clob ,
col_61 blob ,col_62 date ,col_63 blob , col_64 varchar(1000),col_65 date,col_66 date[]
) with (storage_type=ustore);

--step2:创建序列;expect:成功
drop sequence if exists sq_ustore_nextday_0047;
create sequence sq_ustore_nextday_0047 increment by 1 start with 100000;

--step3:清空数据;expect:成功
truncate table t_ustore_nextday_0047;

--step4:插入数据;expect:成功
begin
    for i in 1..1000 loop
      insert into t_ustore_nextday_0047 values(
      sq_ustore_nextday_0047.nextval,to_timestamp('2019-01-03 14:14:12','yyyy-mm-dd hh24:mi:ss.ffffff'),true,3.1415926+sq_ustore_nextday_0047.nextval,lpad('abc','5000','a@123&^%djgk'),i,lpad('abc','30','@'),i+1.456789445455,lpad('abc','5000','a@123&^%djgk'),lpad('abc','30','b'),
      false,'010101111111100000000000000',3.1415926+sq_ustore_nextday_0047.nextval,i/4,'010101111111100000000000000',i,i,to_timestamp('2019-01-03 14:14:12','yyyy-mm-dd hh24:mi:ss.ffffff'),'1',(interval '4 5:12:10.222' day to second(3)),
      0,rpad('abc','30','e'),sq_ustore_nextday_0047.nextval,rpad('abc','100','exc'),lpad('abc','1000','a@123&^%djgk'),lpad('abc','5000','a@123&^%djgk'),i/4,sq_ustore_nextday_0047.nextval-99,i*3.1415,to_timestamp('2019-01-03 14:14:12','yyyy-mm-dd hh24:mi:ss.ffffff'),
      to_timestamp('2019-01-03 14:58:54.000000','yyyy-mm-dd hh24:mi:ss.ffffff'),lpad('abc','5000','a@123&^%djgk'),(interval '12' year),rpad('abc','30','&'),i,'010101111111100000000000000',rpad('abc','300','exc'),i/2.15,'0faadb9',lpad('abc','5000','a@123&^%djgk'),
      1.0e+100,3.14+i,i+445.255,rpad('abc','30','&'),lpad('abc','30','&'),rpad('abc','100','&gdsh'),125563.141592,rpad('abc','30','e'),to_timestamp('2019-01-03 14:14:12','yyyy-mm-dd hh24:mi:ss.ffffff'),lpad('abc','5000','a@123汉字&^%djgk'),
      sq_ustore_nextday_0047.nextval+2,-1.79e+100,98*0.99,'010101111111100000000000000',lpad('abc','5000','a@123&^%djgk'),rpad('abc','8000','a@123&^%djgk'),to_timestamp('2019-01-03 14:14:12','yyyy-mm-dd hh24:mi:ss.ffffff'),25563.1415,lpad('abc','3000','a字符串@123&^%djgk'),'010101111111100000000000000',
      '010101111111100000000000000',to_timestamp('2019-01-03 14:14:12','yyyy-mm-dd hh24:mi:ss.ffffff'),'010101111111100000000000000',rpad('abc','1000','&gdsh'),to_timestamp('2019-01-03 15:19:00','yyyy-mm-dd hh24:mi:ss.ffffff') ,array['2019-01-03 15:19:00','2019-02-03 15:19:00']
      );

    end loop;
end;
/

--step5:查询;expect:成功
declare
cur sys_refcursor;
a int;
date1 date;
date2 date;
date3 timestamp;
begin
    select count(*) into a from (select distinct next_day(t1.col_2,1),next_day(t2.col_65,2),next_day(t1.col_57,3) from t_ustore_nextday_0047 t1 join t_ustore_nextday_0047 t2 on t1.col_2=t2.col_2 and next_day(t2.col_2,1)<next_day(t1.col_65,1) where (next_day(t1.col_57,1)-next_day(t2.col_2,2)) <=0 order by next_day(t1.col_2,1) limit 10);
    open cur for select distinct next_day(t1.col_2,1),next_day(t2.col_65,2),next_day(t1.col_57,3) from t_ustore_nextday_0047 t1 join t_ustore_nextday_0047 t2 on t1.col_2=t2.col_2 and next_day(t2.col_2,1)<next_day(t1.col_65,1) where (next_day(t1.col_57,1)-next_day(t2.col_2,2)) <=0 order by next_day(t1.col_2,1),next_day(t2.col_65,2),next_day(t1.col_57,3) limit 10;
            for i in 1..a loop
            fetch cur into date1,date2,date3;
            exit when cur%notfound;
            if (next_day(date1,1)-next_day(date2,1))>1 then
                raise info '%',('1: '||date1||'--'||date2||'--'||date3);
            else
                raise info '%',('2: '||date2||'--'||date1||'--'||date3);
            end if;
        end loop;
    close cur;
end;
/

--step6:清理环境;expect:成功
drop table if exists t_ustore_nextday_0047;
drop sequence if exists sq_ustore_nextday_0047;